<template>

  <body class="home blog social-top">
  <div class="Navbar">
    <Navbar/>
  </div>
  <div id="content" class="site-content">
    <div class="b2-con">
      <div class="wrapper">
        <div class="home-row-left ">
          <el-carousel width="645px" height="500px" :autoplay="true">
            <el-carousel-item v-for="(item, index) in slider_posts" :key="index">
              <a :href="`/detail/${item.link}`" target="_blank" class="carousel-link">
                <el-image :src="item.image" alt="" fit="contain" class="carousel-image"/>
                <div class="text-overlay">{{ item.text }}</div>
              </a>
            </el-carousel-item>
          </el-carousel>
        </div>

        <div class="home-row-right">
          <div class="home-right">
            <a class="widget-area" v-for="(item, index) in slider_rigin" :key="index" :href="`/detail/${item.link}`">
              <div class="sidebar">
                <el-image :src="item.imageSrc" fit="cover" :alt="item.title" class="sidebar-image"/>
                <h3 class="sidebar-title">{{ item.title }}</h3>
              </div>
            </a>
          </div>
          <div class="home-rights">
            <h4 class="home-rights-title">{{acemen.description ||'所有的伟大，都源于一个勇敢的开始'}}</h4>
          </div>
        </div>

      </div>
    </div>
    <div class="collection-container">
      <div></div>
      <div class="collection-header">
        <a class="collection-card" v-for="item in slider_button" :key="item.id" :href="`/detail/${item.link}`">
          <div class="collection-image">
            <img :src="item.imageSrc" :alt="item.alt"/>
          </div>
          <div class="collection-info">
            <h2 class="collection-title">
              {{ item.title }}
            </h2>
            <div class="collection-updates">
              <span>更新{{ item.updates }}篇</span>
            </div>

          </div>
        </a>
      </div>
      <div class="collection-more" style="    margin-left: -60px;">
        <div class="sign-in-rewards">
          <div class="reward-tabs">
            <button :class="{'active': activeTab === 'today'}" @click="activeTab = 'today'">今日签到</button>
            <button :class="{'active': activeTab === 'consecutive'}" @click="activeTab = 'consecutive'">连续签到
            </button>
          </div>
          <div class="reward-list">
            <div v-for="user in filteredUsers" :key="user.id" class="reward-item">
              <a :href="user.profileLink" class="avatar-parent">
                <el-image :src="user.avatar" class="b2-radius avatar"/>
              </a>
              <div class="user-mission-info">
                <div class="user-mission-info-left">
                  <a :href="user.profileLink">
                    <p>{{ user.username }}</p>
                  </a>
                  <p>
                    <time :datetime="user.time" v-text="timeAgo(user.time)"></time>
                  </p>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
      <div style="margin-left: 10px">

      </div>
    </div>

  </div>
  <div class="footer">
    <cardlrow/>
  </div>
  <div class="back-to-button">
    <row_crd/>
  </div>
  <div class="but">
    <footers/>
  </div>

  </body>
</template>

<script>
import {useUserStore} from "../stores/user.js";
import {posrsStore} from "../stores/index.js";
import {acementStore} from "@/stores/a_cement.js";


export default {

  name: 'Home',
  data() {
    return {
      postsStore: posrsStore(),
      acement:acementStore(),
      activeTab: 'today',
      users: [],
    };
  },
  methods: {
    timeAgo(time) {
      const date = new Date(time);
      const now = new Date();
      const diff = Math.floor((now - date) / 1000 / 60);
      if (diff < 60) {
        return `${diff} 分钟前`;
      } else if (diff < 1440) {
        return `${Math.floor(diff / 60)} 小时前`;
      } else {
        return `${Math.floor(diff / 1440)} 天前`;
      }
    },
    printStyledMessage() {
      console.log(
          "%cDIMTOWN Designed by 樱花小镇 %c",
          "background: linear-gradient(to right, #6a11cb 0%, #2575fc 100%); color: white; padding: 5px; border-radius: 3px;",
          " padding: 2px; margin: 0 5px;",
      );

    },
    loadPosts() {
      this.postsStore.fetchUserInfo(); // 加载本地存储中的信息
    },
    register() {
      this.$refs.registerForm.validate(async (valid) => {
        if (valid) {
          const isSuccess = await this.postsStore.fetchUserInfo(); // 等待 Promise 完成

          if (isSuccess) {


          } else {
          }
        } else {

          return false;
        }
      });
    },


  },
  computed: {
    acemen() {
      if(this.acement.acement==null)
      {
        this.acement.loadacementFromLocalStorage()
      }
      return this.acement.acement;
    },

    slider_rigin() {
      const items = this.postsStore.slider_rigin;
      return items.map(item => {
        return {
          imageSrc: item.imageSrc, // 获取第一条图片 URL
          title: item.title,
          link: item.id,
        };
      });
    },
    slider_posts() {
      const items = this.postsStore.slider_posts;
      return items.map(item => {
        return {
          image: item.imageSrc,
          text: item.title,
          link: item.id,
        };
      });
    },
    slider_button() {
      const items = this.postsStore.slider_button;
      console.log(items, 'items');
      return items.map(item => {
        return {
          imageSrc: item.imageSrc, // 获取第一条图片 URL
          title: item.title,
          alt: item.title,
          link: item.id,
        };
      });
    },
    filteredUsers() {
      return this.users.filter(user => {
        if (this.activeTab === 'today') {
          const today = new Date();
          const date = new Date(user.time);
          return today.toDateString() === date.toDateString();
        } else {
          // Implement logic for consecutive sign-ins if needed
          return true;
        }
      });
    },

  },
  mounted() {
    this.printStyledMessage();

    this.loadPosts();
    this.acement.loadacementFromLocalStorage()

  }
};
</script>

<style scoped>
@media (max-width: 600px) {
  body {
    margin-top: 0;
    width: 100%;
  }

  .Navbar {
  //display: none;

  }

  .el-col-10 {
    /* flex: 0 0 41.6666666667%; */
    /* max-width: 41.6666666667%; */
  }

  .but {
    margin-top: 30px;
    max-width: 100%;
  }

  .site-content {
    margin-top: 2rem;
  }

  .wrapper {
    width: 100%;

  }

  .collection-card {
    margin: 10rem 0;

  }

  .collection-image img {
    max-width: 100%;
  }

  .home-row-right {
    margin: 2rem 0;
    display: flex;
    flex-direction: row;
    width: 100%;
  }

  .home-row-left img {
  //width: 100%; border-radius: 1rem;
  }

  .collection-more {
    display: none;
  }

  .home-rights {
    display: none;
  }

  .home-rights-title {
    display: none;
  }

  .home-row-left {
    width: 100%;
    overflow: hidden; /* 隐藏滚动条 */
  }

  .home-row-left img {
    width: 100%;
    border-radius: 1rem;
  }
}

@media (min-width: 1250px) {

  .Navbar {
    width: 100%;
  }

  .but {
    display: flex;
    justify-content: center;
    align-items: center;
    width: 1400px;
    margin: 40px auto;
  }

  .site-content {
    margin-top: 80px;
  }

  .wrapper {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
  }

  .footer {
    display: flex;
    justify-content: center;
    align-items: center;
  }

  .widget-area {
    display: flex;
    flex-direction: column;
    align-items: center;
    height: 240px;
  }

  .home-right {
    display: flex;
    flex-direction: column;
    align-items: center;
  }

  .home-rights {
    display: flex;
    flex-direction: column;
    align-items: center;
    width: 300px;
    height: 400px;
    margin-left: 20px;
    border-radius: 10px;
    background-size: 50% 50%;
    border: 1px solid rgba(255, 255, 255, 0.18);
    text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.5);
    background-color: rgba(255, 255, 255, 0.2);
    backdrop-filter: blur(10px) saturate(180%);
    padding: 30px;
    text-align: center;
    box-shadow: 0 0 20px rgba(0, 0, 0, 0.1);
  }

  .home-row-right {
    margin-left: 20px;
    display: flex;
    justify-content: center;
    align-items: center;
  }

  .home-row-left {
    backdrop-filter: blur(3px) brightness(88%);
    background-color: rgba(255, 255, 255, 0.34);
    margin: 10px 0;
    width: 645px;
    height: 500px;
    padding: 8px;
    border-radius: 8px;
  }

  .carousel-image {
    max-width: 100%;
    max-height: 100%;
    border-radius: 10px; /* 添加圆角 */
    object-fit: cover; /* 确保图片覆盖整个容器并保持比例 */
  }

  .text-overlay {
    position: absolute;
    bottom: 10px;
    left: 10px;
    color: white;
    background-color: rgba(0, 0, 0, 0.5);
    padding: 5px 10px;
    border-radius: 5px;
  }

  .sidebar-image {
    width: 300px;
    height: 200px;
    border-radius: 10px;
    object-fit: cover;
  }

  .sidebar-title {
    font-size: 16px;
    margin-top: -3px;
    margin-left: 5px;
  }

  .collection-container {
    display: flex;
    justify-content: space-evenly;
    width: 100%;
  }

  .collection-header {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-evenly;
    align-items: center;
  }

  .collection-card {
    display: flex;
    flex-direction: column;
    align-items: center;
    border-radius: 8px;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
    max-width: 300px;
    overflow: hidden;
    position: relative;
    margin: 10px;
    height: 440px;

  }

  .collection-image {
    position: relative;
    height: 340px;
  }

  .collection-number {
    position: absolute;
    top: 10px;
    left: 10px;
    background: rgba(255, 0, 0, 0.7);
    color: rgba(255, 255, 255, 0.69);
    padding: 5px;
    border-radius: 3px;
  }

  .link-block {
    display: block;
    height: 100%;
    width: 100%;
    position: absolute;
    top: 0;
    left: 0;
    z-index: 1;
  }

  .collection-image img {
    width: 290px;
    height: 300px;
  }

  .collection-info {
    padding: 15px;
  }

  .collection-title {
    color: #333;
  }

  .collection-info h2 {
    margin: 0;
    font-size: 20px;
  }

  .collection-updates {
    display: flex;
    justify-content: space-between;
    margin: 10px 0;
  }


  .detail-item img {
    width: 50px;
    height: auto;
    border-radius: 4px;
    margin-right: 10px;
  }


  .sign-in-rewards {
  //width: 100%; width: 300px; margin: 0 auto; background: #fff; border-radius: 8px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1); overflow: hidden; padding: 20px; background-color: rgba(255, 255, 255, 0.67);
  }


  .reward-header i {
    margin-right: 10px;
  }

  .reward-tabs {
    display: flex;
    justify-content: space-between;
    margin-bottom: 10px;
    border-radius: 50%;
    margin-right: 10px;
  }

  .reward-tabs button {
    flex: 1;
    padding: 10px;
    cursor: pointer;
    background: none;
    border: none;
    outline: none;
    font-size: 16px;
    border-radius: 8px;
    transition: background 0.3s ease;
  }

  .reward-tabs button.active {
    background: orange;
  //color: white;
  }

  .reward-list {
    max-height: 400px;
    overflow-y: auto;
  }

  .reward-item {
    display: flex;
    align-items: center;
    padding: 5px 0;
    border-bottom: 1px solid #f0f0f0;
    border-radius: 8px;
  }


  .avatar {
    width: 50px;
    height: 50px;
    margin-right: 10px;
  }

  .user-mission-info {
    display: flex;
    justify-content: space-between;
    align-items: center;
    width: 100%;
  }

  .user-mission-info-left p {
    margin: 0;
  }


  .user-credit i {
    margin-right: 5px;
  }

  .back-to-button {
    display: flex;
    justify-content: center;
    align-items: center;
  }

}
</style>
